#include "tools.h"

using std::string;

///////////////////////////////// string formatting //////////////////////////////
StringFormater::operator string() const
{
  std::ostringstream os;
  
  size_t pos = 0;
  while (pos < m_fmt.size())
  {
    if (m_fmt[pos] != '{')
    {
      os << m_fmt[pos++];
      continue;
    }

    // parse int in "{}"
    size_t p1 = pos + 1;
    unsigned int i = 0;
    while ( p1 < m_fmt.size() && isdigit(m_fmt[p1]) )
    {
      i = i * 10 + m_fmt[p1++] - '0';
      if (i >= m_params.size())
      {
        os << m_fmt[pos++];
        continue;
      }
    }

    if (p1 >= m_fmt.size() || m_fmt[p1] != '}')
    {
      os << m_fmt[pos++];
      continue;
    }

    os << m_params[i];
    pos = p1 + 1;
  }

  return os.str();
}

StringFormater format(string fmt)
{
  return StringFormater(fmt);
}
